
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Web.Administration
{
    public partial class Administration_ProductVariantAdd : BaseNopAdministrationPage
    {
        private void BindData()
        {
            Product product = ProductManager.GetByProductID(this.ProductID);
            if (product == null)
                Response.Redirect("Products.aspx");
            lblProductName.Text =Server.HtmlEncode(product.Name);
            hlProductURL.NavigateUrl = "ProductDetails.aspx?ProductID=" + product.ProductID;
            DiscountMappingControl.BindData();
        }

        private void FillDropDowns()
        {
            this.ddlTaxCategory.Items.Clear();
            ListItem itemTaxCategory = new ListItem("Not Taxable", "0");
            this.ddlTaxCategory.Items.Add(itemTaxCategory);
            TaxCategoryCollection taxCategoryCollection = TaxCategoryManager.GetAllTaxCategories();
            foreach (TaxCategory taxCategory in taxCategoryCollection)
            {
                ListItem item2 = new ListItem(taxCategory.Name, taxCategory.TaxCategoryID.ToString());
                this.ddlTaxCategory.Items.Add(item2);
            }

            this.ddlWarehouse.Items.Clear();
            ListItem itemWarehouse = new ListItem("---", "0");
            this.ddlWarehouse.Items.Add(itemWarehouse);
            WarehouseCollection warehouseCollection = WarehouseManager.GetAllWarehouses();
            foreach (Warehouse warehouse in warehouseCollection)
            {
                ListItem item2 = new ListItem(warehouse.Name, warehouse.WarehouseID.ToString());
                this.ddlWarehouse.Items.Add(item2);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillDropDowns();
                BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Product product = ProductManager.GetByProductID(this.ProductID);
                    if (product != null)
                    {
                        Picture productVariantPicture = null;
                        HttpPostedFile productVariantPictureFile = fuProductVariantPicture.PostedFile;
                        if ((productVariantPictureFile != null) && (!String.IsNullOrEmpty(productVariantPictureFile.FileName)))
                        {
                            byte[] productVariantPictureBinary = PictureManager.GetPictureBits(productVariantPictureFile.InputStream, productVariantPictureFile.ContentLength);
                            productVariantPicture = PictureManager.InsertPicture(productVariantPictureBinary, productVariantPictureFile.ContentType, true);
                        }
                        int productVariantPictureID = 0;
                        if (productVariantPicture != null)
                            productVariantPictureID = productVariantPicture.PictureID;

                        Download productVariantDownload = null;
                        HttpPostedFile productVariantDownloadFile = fuProductVariantDownload.PostedFile;
                        if ((productVariantDownloadFile != null) && (!String.IsNullOrEmpty(productVariantDownloadFile.FileName)))
                        {
                            byte[] productVariantDownloadBinary = DownloadManager.GetDownloadBits(productVariantDownloadFile.InputStream, productVariantDownloadFile.ContentLength);
                            productVariantDownload = DownloadManager.InsertDownload(productVariantDownloadBinary, productVariantDownloadFile.ContentType, true);
                        }
                        int productVariantDownloadID = 0;
                        if (productVariantDownload != null)
                            productVariantDownloadID = productVariantDownload.DownloadID;

                        DateTime nowDT = DateTime.Now;
                        ProductVariant productVariant = ProductVariantManager.InsertProductVariant(product.ProductID,
                             txtName.Text, txtSKU.Text, txtDescription.Value, txtAdminComment.Text, txtManufacturerPartNumber.Text,
                             cbIsDownload.Checked, productVariantDownloadID, cbIsShipEnabled.Checked, cbIsFreeShipping.Checked,
                             int.Parse(this.ddlTaxCategory.SelectedItem.Value), txtStockQuantity.Value,
                              int.Parse(this.ddlWarehouse.SelectedItem.Value), cbDisableBuyButton.Checked, cbRequiresTextOption.Checked, txtTextOptionPrompt.Text,
                             txtPrice.Value, txtOldPrice.Value, txtWeight.Value, txtLength.Value,
                             txtWidth.Value, txtHeight.Value, productVariantPictureID, cbPublished.Checked,
                             false, txtDisplayOrder.Value, nowDT, nowDT);

                        foreach (int discountID in DiscountMappingControl.SelectedDiscountIDs)
                            DiscountManager.AddDiscountToProductVariant(productVariant.ProductVariantID, discountID);

                        Response.Redirect("ProductVariantDetails.aspx?ProductVariantID=" + productVariant.ProductVariantID);
                    }
                    else
                        Response.Redirect("Products.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        public int ProductID
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductID");
            }
        }
    }
}